package com.zzy.collection;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.ObjectArrays;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
/**
 * 数组的排序
 * @author guokaige
 *
 */
public class CollectionTest {

	public static void main(String[] args) {
		System.out.println("HHHHHHH");
	}

	@Test
	public void testCreate() {
		List<Person> personList = Lists.newLinkedList();
		Set<Person> personSet = Sets.newHashSet();
		Map<String, Person> personMap = Maps.newHashMap();
		Integer[] intArrays = ObjectArrays.newArray(Integer.class, 10);

		Set<String> set = Sets.newHashSet("one", "two", "three");

		List<String> list = Lists.newArrayList("one", "two", "three");

		Map<String, String> map = ImmutableMap.of("ON", "TRUE", "OFF", "FALSE");
		// 2,简化集合的初始化
		List<Person> personList2 = Lists.newArrayList(
				new Person("张三", "46546"), 
				new Person("李四", "46546"));

		
	}
	
	
	/**
	 * 不可变集合
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用)     
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年11月14日 上午9:20:53
	 */
	public void TestImmutable(){
		ImmutableSet<String> foobar = ImmutableSet.of("foo", "bar", "baz");
		ImmutableList<String> defensiveCopy = ImmutableList.copyOf(new ArrayList<String>());
		
		
		Map<String, String> map1 = ImmutableMap.of("key1","val1","key2","val2");
		Map<String, String> map2 = ImmutableMap.copyOf(new HashMap<String,String>());
		
		//普通方式
		Map map = new HashMap(); 
		Map unmodifiableMap = Collections.unmodifiableMap(map);
	}
	
	
	
	
	
	
	

	/**
	 * 测试排序
	 * @Title:        title
	 * @Description:  TODO(这里用一句话描述这个方法的作用)     
	 * @version       V3.0 
	 * @author        guokaige@topnet.net.cn
	 * @Date          2016年10月31日 下午2:09:00
	 */
	@Test
	public  void testOrdering() {
	
		        List<Integer> numbers = Lists.newArrayList(30, 20, 60, 80, 10);
				List<Integer> result=Lists.newArrayList();
				
				result=Ordering.natural().sortedCopy(numbers); // 10,20,30,60,80

				result=Ordering.natural().reverse().sortedCopy(numbers); // 80,60,30,20,10

				Integer min=Ordering.natural().min(numbers); // 10

				Integer max=Ordering.natural().max(numbers); // 80

				Lists.newArrayList(30, 20, 60, 80, null, 10);

				Ordering.natural().nullsLast().sortedCopy(numbers); // 10,
																	// 20,30,60,80,null

				Ordering.natural().nullsFirst().sortedCopy(numbers); // null,10,20,30,60,80
				
				//natural()	对可排序类型做自然排序，如数字按大小，日期按先后排序
				//usingToString()	按对象的字符串形式做字典排序[lexicographical ordering]
				//from(Comparator)	把给定的Comparator转化为排序器
				//reverse()	获取语义相反的排序器
				//nullsFirst()	使用当前排序器，但额外把null值排到最前面。
				//nullsLast()	使用当前排序器，但额外把null值排到最后面。
				
			////////////////////////////////////////////////////////////////////////	
				
		//自定义排序		
		List<Person> personList = Lists.newArrayList(new Person(1, "张三", "46546"),
				new Person(2, "李四", "46546"),
				new Person(3, "王五", "46546"), 
				new Person(4, "赵六", "46546"),
				new Person(5, "钱七", "46546"));

		
		
		Ordering<Person> byIDOrder = new Ordering<Person>() {
			@Override
			public int compare(Person left, Person right) {
				return right.getId() - left.getId();   //大于1  
			}
		};

		
		//便利
		for (Person p : byIDOrder.immutableSortedCopy(personList)) {
			System.out.println(p.getName());
		}
	}

}
